﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using LY.Common.Utils.Sf_Struct;

namespace LY.ShengPayApi.Services
{
   public class Delete
    {
       /// <summary>
       /// 解约用户代扣协议
       /// </summary>
       /// <param name="serverUrl">盛付通访问地址</param>
       /// <param name="merchantNo">平台商户号（由盛付通分配，请联系盛付通商务获取）</param>
       /// <param name="userName">平台用户账户（由平台生成，并在平台中保证唯一）</param>
       /// <returns></returns>
       public static string DeleteUserWithHoid(string serverUrl, string merchantNo, string userName)
       {
           string[] mysign = new string[2];
           mysign[0] = "merchantNo=" + merchantNo;
           mysign[1] = "userName=" + userName;
           Array array = mysign;
           Array.Sort(array);
           string sign = string.Join("&", mysign);
           MySign mySign = new MySign();
           //获得签名。封装号的方法
           string temp = mySign.GetSign(sign);
           string newsign = HttpContext.Current.Server.UrlEncode(temp);
           string postString = "?sign=" + newsign;
           string url = serverUrl + "/userAccount/" + merchantNo + "-" + userName + "/agreement" + postString; //userAccount/{merchantNo}-{userName}/agreement
           return HttpDelete(url, postString);
       }

       /// <summary>
       /// 撤销用户投标
       /// </summary>
       /// <param name="serverUrl"></param>
       /// <param name="merchantNo">平台商户号（由盛付通分配，请联系盛付通商务获取）</param>
       /// <param name="issueBidNo">平台发标编号（由平台生成，并在平台中保证唯一）</param>
       /// <param name="submitBidNo">平台投标编号（由平台生成，并在平台中保证唯一）</param>
       public static string DeleteP2PBidSubmitBid(string serverUrl, string merchantNo, string issueBidNo, string submitBidNo)
        {
            string[] mysign = new string[3];
            mysign[0] = "merchantNo=" + merchantNo;
            mysign[1] = "issueBidNo=" + issueBidNo;
            mysign[2] = "submitBidNo=" + submitBidNo; 
            Array array = mysign;
            Array.Sort(array);
            string sign = string.Join("&", mysign);
            MySign mySign = new MySign();
            //获得签名。封装号的方法
            string temp = mySign.GetSign(sign);
            string newsign = HttpContext.Current.Server.UrlEncode(temp);  
            string postString = "?" + string.Join("&", mysign) + "&sign=" + newsign;
            string url = serverUrl + "/p2pBid/" + merchantNo + "-" + issueBidNo + "/submitBid" + postString; 
            return HttpDelete(url, postString);
        }

       /// <summary>
       /// 资金解冻（债权收购）
       /// </summary>
       /// <param name="merchantNo">平台商户号（由盛付通分配，请联系盛付通商务获取）</param>
       /// <param name="notifyUrl">异步通知回调地址</param>
       /// <param name="assignmentFundNo">资金冻结订单号（由平台生成，并在平台中保证唯一）</param>
       /// <param name="unfrozenAmount">资金解冻金额（以元为单位，仅允许两位小数。譬如：600.00）</param>
       /// <param name="memo">资金解冻备注</param>
       /// <param name="serverUrl">盛付通访问地址</param>
       /// <returns></returns>
       public static string DeleteDeblockFund(string merchantNo, string notifyUrl, string assignmentFundNo, string unfrozenAmount, string memo, string serverUrl)
       {
           string[] mysign = new string[6];
           mysign[0] = "merchantNo=" + merchantNo;
           mysign[1] = "timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmss");
           mysign[2] = "notifyUrl=" + notifyUrl;
           mysign[3] = "assignmentFundNo=" + assignmentFundNo;
           mysign[4] = "unfrozenAmount=" + unfrozenAmount;
           mysign[5] = "memo=" + memo;
           Array array = mysign;
           Array.Sort(array);
           string sign = string.Join("&", mysign);
           MySign mySign = new MySign();
           //获得签名。封装号的方法
           string temp = mySign.GetSign(sign);
           string newsign = HttpContext.Current.Server.UrlEncode(temp);
           string postString = "?" + string.Join("&", mysign) + "&sign=" + newsign;
           string url = serverUrl + "/assignmentFund/" + merchantNo + "-" + assignmentFundNo + postString; //assignmentFund/{merchantNo}-{assignmentFundNo}
           return HttpDelete(url, postString);
       }

       /// <summary>
       /// 撤销融资订单
       /// </summary>
       /// <param name="merchantNo">平台商户号（由盛付通分配，请联系盛付通商务获取）</param>
       /// <param name="financingNo">融资订单号（由平台生成，并在平台中保证唯一）</param>
       /// <param name="userName">融资人（由平台生成，并在平台中保证唯一）</param>
       /// <param name="securityAmount">保证金金额（以元为单位，仅允许两位小数）</param>
       /// <param name="serverUrl">盛付通访问地址</param>
       /// <returns></returns>
       public static string DeleteCancelFinancingOrder(string merchantNo, string financingNo, string userName, string securityAmount, string serverUrl)
       {
           string[] mysign = new string[5];
           mysign[0] = "merchantNo=" + merchantNo;
           mysign[1] = "timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmss");
           mysign[2] = "financingNo=" + financingNo;
           mysign[3] = "userName=" + userName;
           mysign[4] = "securityAmount=" + securityAmount;
           Array array = mysign;
           Array.Sort(array);
           string sign = string.Join("&", mysign);
           MySign mySign = new MySign();
           //获得签名。封装号的方法
           string temp = mySign.GetSign(sign);
           string newsign = HttpContext.Current.Server.UrlEncode(temp);
           string postString = "?" + string.Join("&", mysign) + "&sign=" + newsign;
           string url = serverUrl + "/financing/" + merchantNo + "-" + financingNo + postString; //financing/{merchantNo}-{financingNo}
           return HttpDelete(url, postString);
       }

        private static string HttpDelete(string url, string param)
        {  
            using (var client = new WebClient())
            {
                byte[] postData = Encoding.ASCII.GetBytes(param);
                byte[] responseData= client.UploadData(url, "DELETE", postData);
                return Encoding.UTF8.GetString(responseData); 
            } 
        }
    }
}
